package com.cwvs.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.cwvs.utils.AppUtil;
import com.cwvs.utils.CommonUtils;
import com.cwvs.utils.JsonUtil;
import com.cwvs.utils.MySqlPageUtil;
import com.cwvs.utils.Pages;
import com.pingplusplus.model.Charge;

/**
 * 
 * 订单相关DAO
 */
@Repository
public class AppOrderDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private MySqlPageUtil mySqlPageUtil;

	/**
	 * 主订单生成
	 * 
	 * @param m
	 * @return
	 */
	public Map<String, Object> createOrder(Map<String, Object> m) {
		int date = Integer.parseInt(AppUtil.getStringDate());
		String orderNo = orderNo(date);
		StringBuffer sbf = new StringBuffer();
		sbf.append(" insert into app_order_main (");
		sbf.append(" id,order_no,order_date,type_id");
		sbf.append(" ,type_name,status,status_name,remark ");
		sbf.append(" ,ctime,utime ,token_id,user_id,contactor_person,contactor_tel,title)");
		sbf.append(" values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		String id = UUID.randomUUID().toString();
		List<Object> ls = new ArrayList<Object>();
		ls.add(id);
		ls.add(orderNo);
		ls.add(date);
		ls.add(MapUtils.getString(m, "TYPE_ID"));
		ls.add(MapUtils.getString(m, "TYPE_NAME"));
		ls.add(1);
		ls.add("待处理");
		ls.add(MapUtils.getString(m, "REMARK"));
		ls.add(new Date());
		ls.add(new Date());
		ls.add(MapUtils.getString(m, "TOKEN"));
		ls.add(MapUtils.getString(m, "USER_ID"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PERSON"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PHONE"));
		ls.add(MapUtils.getString(m, "TITLE"));
		m.put("ORDER_NO", orderNo);
		m.put("ID", id);
		jdbcTemplate.update(sbf.toString(), ls.toArray());
		// 预订订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"YDDD")) {
			bookOrder(m);
		}
		// 外卖订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"WMDD")) {
			takeOutOrder(m);
		}
		// 同城订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"TCDD")) {
			sameCityOrder(m);
		}
		// 老人专区(陪聊/买菜/打扫)
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"LRMC")
				|| StringUtils.equalsIgnoreCase(
						MapUtils.getString(m, "TYPE_CODE"), "LRPL")
				|| StringUtils.equalsIgnoreCase(
						MapUtils.getString(m, "TYPE_CODE"), "LRDS")) {
			arrondiOrder(m);
		}
		// 接送
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"LCWDD")) {
			petProductsOrder(m);
		}

		// 陪跑订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"PPDD")) {
			withRunOrder(m);
		}
		// 挂号订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"GHDD")) {
			registerOrder(m);
		}
		// 时间工坊
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"SJGF")) {
			timesWorkOrder(m);
		}
		// 许愿盒子
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"XYHZ")) {
			promiseOrder(m);
		}
		return m;
	}

	/**
	 * 订单号生成
	 * 
	 * @return
	 */
	private String orderNo(int dt) {
		int date = dt;
		String pre = "TW";
		StringBuffer sbf = new StringBuffer();
		sbf.append("select ifnull(max(order_no),'0000') order_no from app_order_main ");
		sbf.append(" where order_date=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { date });
		String orderNo = pre + date + "0000";
		if (!ls.isEmpty()
				&& !StringUtils.equalsIgnoreCase("0000",
						MapUtils.getString(ls.get(0), "ORDER_NO"))) {
			orderNo = MapUtils.getString(ls.get(0), "ORDER_NO");
		}
		String order = orderNo.substring(10);
		int num = Integer.parseInt(order) + 1;
		String max = "";
		if (num < 10) {
			max = "000" + num;
		} else if (num >= 10 && num < 100) {
			max = "00" + num;
		} else if (num >= 100 && num < 1000) {
			max = "0" + num;
		} else {
			max = num + "";
		}
		return pre + date + max;
	}

	/**
	 * 生成预订订单
	 */
	private void bookOrder(Map<String, Object> m) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" insert into app_order_book (");
		sbf.append(" id,store_id,reservation_time,reservation_person, ");
		sbf.append(" reservation_phone,reservation_name )");
		sbf.append(" values(?,?,?,?,?,?)");
		List<Object> ls = new ArrayList<>();
		ls.add(MapUtils.getString(m, "ID"));
		ls.add(MapUtils.getString(m, "STORE_ID"));
		ls.add(MapUtils.getString(m, "RESERVATION_TIME"));
		ls.add(MapUtils.getString(m, "RESERVATION_PERSON"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PHONE"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PERSON"));
		jdbcTemplate.update(sbf.toString(), ls.toArray());
	}

	/**
	 * 生成外卖订单
	 */
	private void takeOutOrder(Map<String, Object> m) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" insert into app_take_out_order (");
		sbf.append(" id,store_address,reservation_time, ");
		sbf.append(" reservation_phone,reservation_name,reservation_address )");
		sbf.append(" values(?,?,?,?,?,?)");
		List<Object> ls = new ArrayList<>();
		ls.add(MapUtils.getString(m, "ID"));
		ls.add(MapUtils.getString(m, "STORE_ADDRESS"));
		ls.add(MapUtils.getString(m, "RESERVATION_TIME"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PHONE"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PERSON"));
		ls.add(MapUtils.getString(m, "CONTACTOR_ADDRESS"));
		jdbcTemplate.update(sbf.toString(), ls.toArray());
	}

	/**
	 * 同城订单
	 */
	private void sameCityOrder(Map<String, Object> m) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" insert into app_same_city_order (");
		sbf.append(" id,phone,name,RESERVATION_TIME )");
		sbf.append(" values(?,?,?,?)");
		List<Object> ls = new ArrayList<>();
		ls.add(MapUtils.getString(m, "ID"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PHONE"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PERSON"));
		ls.add(MapUtils.getString(m, "RESERVATION_TIME"));
		jdbcTemplate.update(sbf.toString(), ls.toArray());
	}

	/**
	 * 许愿盒子订单
	 */
	private void promiseOrder(Map<String, Object> m) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" insert into app_promise_order (");
		sbf.append(" id,phone,name )");
		sbf.append(" values(?,?,?)");
		List<Object> ls = new ArrayList<>();
		ls.add(MapUtils.getString(m, "ID"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PHONE"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PERSON"));
		jdbcTemplate.update(sbf.toString(), ls.toArray());
	}
	

	/**
	 * 陪跑订单
	 */
	private void withRunOrder(Map<String, Object> m) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" insert into app_with_run_order (");
		sbf.append(" id,phone,name,RESERVATION_TIME,ADDRESS )");
		sbf.append(" values(?,?,?,?,?)");
		List<Object> ls = new ArrayList<>();
		ls.add(MapUtils.getString(m, "ID"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PHONE"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PERSON"));
		ls.add(MapUtils.getString(m, "RESERVATION_TIME"));
		ls.add(MapUtils.getString(m, "CONTACTOR_ADDRESS"));
		jdbcTemplate.update(sbf.toString(), ls.toArray());
	}

	/**
	 * 老人专区订单
	 */
	private void arrondiOrder(Map<String, Object> m) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" insert into app_arrondi_order (");
		sbf.append(" id,phone,name,address,age,RESERVATION_TIME )");
		sbf.append(" values(?,?,?,?,?,?)");
		List<Object> ls = new ArrayList<>();
		ls.add(MapUtils.getString(m, "ID"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PHONE"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PERSON"));
		ls.add(MapUtils.getString(m, "CONTACTOR_ADDRESS"));
		ls.add(MapUtils.getInteger(m, "AGE"));
		ls.add(MapUtils.getString(m, "RESERVATION_TIME"));
		jdbcTemplate.update(sbf.toString(), ls.toArray());
	}

	/**
	 * 接送服务
	 */
	private void petProductsOrder(Map<String, Object> m) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" insert into app_pet_products_order (");
		sbf.append(" id,phone,name,RESERVATION_TIME,ADDRESS,SEND_ADDRESS ,CAR_TYPE)");
		sbf.append(" values(?,?,?,?,?,?,?)");
		List<Object> ls = new ArrayList<>();
		ls.add(MapUtils.getString(m, "ID"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PHONE"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PERSON"));
		ls.add(MapUtils.getString(m, "RESERVATION_TIME"));
		ls.add(MapUtils.getString(m, "CONTACTOR_ADDRESS"));
		ls.add(MapUtils.getString(m, "SEND_ADDRESS"));
		ls.add(MapUtils.getString(m, "CAR_TYPE"));
		jdbcTemplate.update(sbf.toString(), ls.toArray());
	}

	/**
	 * 挂号订单
	 */
	private void registerOrder(Map<String, Object> m) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" insert into app_register_order (");
		sbf.append(" id,phone,name,reservation_time,store_id,department_id,person_id )");
		sbf.append(" values(?,?,?,?,?,?,?)");
		List<Object> ls = new ArrayList<>();
		ls.add(MapUtils.getString(m, "ID"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PHONE"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PERSON"));
		ls.add(MapUtils.getString(m, "RESERVATION_TIME"));
		ls.add(MapUtils.getString(m, "STORE_ID"));
		ls.add(MapUtils.getString(m, "DEPARTMENT_ID"));
		ls.add(MapUtils.getString(m, "PERSON_ID"));
		jdbcTemplate.update(sbf.toString(), ls.toArray());
	}

	/**
	 * 时间工坊订单
	 */
	private void timesWorkOrder(Map<String, Object> m) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" insert into app_times_work_order (");
		sbf.append(" id,phone,name,RESERVATION_TIME,ADDRESS )");
		sbf.append(" values(?,?,?,?,?)");
		List<Object> ls = new ArrayList<>();
		ls.add(MapUtils.getString(m, "ID"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PHONE"));
		ls.add(MapUtils.getString(m, "CONTACTOR_PERSON"));
		ls.add(MapUtils.getString(m, "RESERVATION_TIME"));
		ls.add(MapUtils.getString(m, "CONTACTOR_ADDRESS"));
		jdbcTemplate.update(sbf.toString(), ls.toArray());
	}

	/**
	 * 通过订单类型获取订单类型信息
	 */
	public Map<String, Object> getOrderTypeByCode(String code) {
		if (StringUtils.isBlank(code)) {
			return null;
		}
		List<Map<String, Object>> ls = jdbcTemplate
				.queryForList(
						"SELECT ID,CODE,NAME,REMARK FROM APP_ORDER_TYPE WHERE CODE=? AND IS_ACTIVE=1 ",
						new Object[] { code });

		if (!ls.isEmpty()) {
			return ls.get(0);
		}
		return null;
	}

	/**
	 * 外卖类型列表
	 */
	public List<Map<String, Object>> takeOutList() {
		return jdbcTemplate
				.queryForList("SELECT ID,CODE,NAME FROM APP_TAKE_OUT_TYPE WHERE IS_ACTIVE=1 ");
	}

	/**
	 * 通过外卖CODE获取外卖信息
	 */
	public Map<String, Object> getTakeOutByCode(String code) {
		if (StringUtils.isBlank(code)) {
			return null;
		}
		List<Map<String, Object>> ls = jdbcTemplate
				.queryForList("SELECT ID,CODE,NAME FROM APP_TAKE_OUT_TYPE WHERE IS_ACTIVE=1 WHERE CODE=? ");
		if (!ls.isEmpty()) {
			return ls.get(0);
		}
		return null;
	}

	/**
	 * 获取个人订单列表
	 */
	public List<Map<String, Object>> getOrderListByUser(String uid,
			String[] status, int cpage) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("SELECT ID,TYPE_ID ORDER_TYPE,TITLE,ORDER_NO,ORDER_DATE,STATUS,STATUS_NAME,REMARK,PRICE,date_format(CTIME,'%Y-%m-%d %T') C_TIME,date_format(UTIME,'%Y-%m-%d %T') U_TIME FROM APP_ORDER_MAIN WHERE USER_ID=? ");
		List<Object> params = new ArrayList<Object>();
		params.add(uid);
		if (status.length != 0) {
			sbf.append("AND (1=2 ");
			for (int i = 0; i < status.length; i++) {
				params.add(Integer.parseInt(status[i]));
				sbf.append(" OR STATUS= ? ");
			}

			sbf.append(" ) ");
		}
		sbf.append(" ORDER BY ORDER_NO DESC ");
		Pages page = new Pages();
		page.setTotalRecord(getOrderCountByStatus(uid, status));
		page.setCurrentPage(cpage);
		String sql = mySqlPageUtil.getPageSql(page, sbf.toString());
		return jdbcTemplate.queryForList(sql, params.toArray());

	}

	/**
	 * 订单总数
	 */
	public int getOrderCountByStatus(String uid, String[] status) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT COUNT(1) FROM APP_ORDER_MAIN WHERE USER_ID=? ");
		List<Object> params = new ArrayList<Object>();
		params.add(uid);
		if (status.length != 0) {
			sbf.append("AND (1=2 ");
			for (int i = 0; i < status.length; i++) {
				params.add(Integer.parseInt(status[i]));
				sbf.append(" OR STATUS= ? ");
			}
			sbf.append(" ) ");
		}
		return jdbcTemplate.queryForInt(sbf.toString(), params.toArray());
	}

	/**
	 * 订单列表
	 * 
	 * @return
	 */
	public Pages orderList(Map<String, Object> m, Pages page) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT T1.ID,T1.ORDER_NO,T1.ORDER_DATE,T1.TYPE_ID,T1.TITLE ");
		sql.append(",T1.TYPE_NAME,T1.STATUS,T1.STATUS_NAME,REMARK ");
		sql.append(",T1.CTIME,T1.UTIME,T1.PRICE,T1.USER_ID,T1.TOKEN_ID ");
		sql.append(",T1.CONTACTOR_PERSON,T1.CONTACTOR_TEL ");
		sql.append(" FROM APP_ORDER_MAIN T1 ");
		sql.append("  WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		if (StringUtils.isNotBlank(MapUtils.getString(m, "ORDER_DATE"))) {
			params.add(MapUtils.getString(m, "ORDER_DATE").replaceAll("-", ""));
			sql.append(" AND T1.ORDER_DATE=? ");
		}
		if (StringUtils.isNotBlank(MapUtils.getString(m, "ORDER_NO"))) {
			params.add("%"+MapUtils.getString(m, "ORDER_NO").toUpperCase()+"%");
			sql.append(" AND upper(T1.ORDER_NO) like ? ");
		}
		if (StringUtils.isNotBlank(MapUtils.getString(m, "CONTACTOR_PERSON"))) {
			params.add("%"+MapUtils.getString(m, "CONTACTOR_PERSON").toUpperCase()+"%");
			sql.append(" AND upper(T1.CONTACTOR_PERSON) like ? ");
		}
		if (StringUtils.isNotBlank(MapUtils.getString(m, "CONTACTOR_TEL"))) {
			params.add("%"+MapUtils.getString(m, "CONTACTOR_TEL").toUpperCase()+"%");
			sql.append(" AND upper(T1.CONTACTOR_TEL) like ? ");
		}
		if (StringUtils.isNotBlank(MapUtils.getString(m, "ORDER_STATUS"))) {
			params.add(MapUtils.getIntValue(m, "ORDER_STATUS"));
			sql.append(" AND T1.STATUS = ? ");
		}
		sql.append(" ORDER BY T1.UTIME DESC ");
		String page_sql = mySqlPageUtil.getPageSql(page, sql.toString());
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(page_sql,
				params.toArray());
		page.setPage(ls);
		page.setTotalRecord(orderCount(m));
		return page;
	}

	private int orderCount(Map<String, Object> m) {
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(1) FROM APP_ORDER_MAIN T1 WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		if (StringUtils.isNotBlank(MapUtils.getString(m, "ORDER_DATE"))) {
			params.add(MapUtils.getString(m, "ORDER_DATE").replaceAll("-", ""));
			sql.append(" AND T1.ORDER_DATE=? ");
		}
		if (StringUtils.isNotBlank(MapUtils.getString(m, "ORDER_NO"))) {
			params.add(MapUtils.getString(m, "ORDER_NO").toUpperCase());
			sql.append(" AND upper(T1.ORDER_NO) like ? ");
		}
		if (StringUtils.isNotBlank(MapUtils.getString(m, "CONTACTOR_PERSON"))) {
			params.add("%"+MapUtils.getString(m, "CONTACTOR_PERSON").toUpperCase()+"%");
			sql.append(" AND upper(T1.CONTACTOR_PERSON) like ? ");
		}
		if (StringUtils.isNotBlank(MapUtils.getString(m, "CONTACTOR_TEL"))) {
			params.add("%"+MapUtils.getString(m, "CONTACTOR_TEL").toUpperCase()+"%");
			sql.append(" AND upper(T1.CONTACTOR_TEL) like ? ");
		}
		if (StringUtils.isNotBlank(MapUtils.getString(m, "ORDER_STATUS"))) {
			params.add(MapUtils.getIntValue(m, "ORDER_STATUS"));
			sql.append(" AND T1.STATUS = ? ");
		}
		return jdbcTemplate.queryForInt(sql.toString(), params.toArray());
	}

	/**
	 * 通过用户ID和订单ID获取订单详细信息
	 * 
	 * @param uid
	 * @param id
	 * @return
	 */
	public Map<String, Object> orderInfoById(String uid, String id) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT ID,ORDER_NO,ORDER_DATE,TYPE_ID TYPE_CODE,TYPE_NAME,TITLE ");
		sbf.append(" ,STATUS,STATUS_NAME,REMARK,date_format(CTIME,'%Y-%m-%d %T') CTIME,date_format(UTIME,'%Y-%m-%d %T') UTIME ,PRICE ");
		sbf.append(" ,CONTACTOR_PERSON,CONTACTOR_TEL CONTACTOR_PHONE ");
		sbf.append(" FROM APP_ORDER_MAIN ");
		sbf.append(" WHERE USER_ID=? AND ID=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { uid, id });
		if (ls.isEmpty()) {
			return new HashMap<String, Object>();
		}
		Map<String, Object> m = ls.get(0);
		// 预订订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"YDDD")) {
			m.putAll(bookOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 外卖订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"WMDD")) {
			m.putAll(takeOutOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 同城订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"TCDD")) {
			m.putAll(sameCityOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 老人专区(陪聊/买菜/打扫)
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"LRMC")
				|| StringUtils.equalsIgnoreCase(
						MapUtils.getString(m, "TYPE_CODE"), "LRPL")
				|| StringUtils.equalsIgnoreCase(
						MapUtils.getString(m, "TYPE_CODE"), "LRDS")) {
			m.putAll(arrondiOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 接送
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"LCWDD")) {
			 m.putAll(petProductsOrderInfo(MapUtils.getString(m, "ID")));
		}

		// 陪跑订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"PPDD")) {
			 m.putAll(withRunOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 挂号订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"GHDD")) {
			m.putAll(registerOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 时间工坊
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"SJGF")) {
			m.putAll(timesWorkOrderInfo(MapUtils.getString(m, "ID")));
		}
		return m;
	}

	/**
	 * 通过订单ID获取订单信息
	 * 
	 * @param id
	 * @return
	 */
	public Map<String, Object> getOrderInfoById(String id) {

		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT ID,ORDER_NO,ORDER_DATE,TYPE_ID TYPE_CODE,TYPE_NAME,TITLE ");
		sbf.append(" ,STATUS,STATUS_NAME,REMARK,date_format(CTIME,'%Y-%m-%d %T') CTIME,date_format(UTIME,'%Y-%m-%d %T') UTIME ,PRICE ");
		sbf.append(" ,CONTACTOR_PERSON,CONTACTOR_TEL CONTACTOR_PHONE ");
		sbf.append(" FROM APP_ORDER_MAIN ");
		sbf.append(" WHERE  ID=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { id });
		if (ls.isEmpty()) {
			return new HashMap<String, Object>();
		}
		Map<String, Object> m = ls.get(0);
		// 预订订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"YDDD")) {
			m.putAll(bookOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 外卖订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"WMDD")) {
			m.putAll(takeOutOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 同城订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"TCDD")) {
			m.putAll(sameCityOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 老人专区(陪聊/买菜/打扫)
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"LRMC")
				|| StringUtils.equalsIgnoreCase(
						MapUtils.getString(m, "TYPE_CODE"), "LRPL")
				|| StringUtils.equalsIgnoreCase(
						MapUtils.getString(m, "TYPE_CODE"), "LRDS")) {
			m.putAll(arrondiOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 接送订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"LCWDD")) {
			m.putAll(petProductsOrderInfo(MapUtils.getString(m, "ID")));
		}

		// 陪跑订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"PPDD")) {
			m.putAll(withRunOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 挂号订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"GHDD")) {
			m.putAll(registerOrderInfo(MapUtils.getString(m, "ID")));
		}
		// 时间工坊
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"SJGF")) {
			m.putAll(timesWorkOrderInfo(MapUtils.getString(m, "ID")));
			try {
				m.put("ID_CARD", CommonUtils.decrypt(MapUtils.getString(m, "ID_CARD"), "3x9f1298"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 许愿盒子
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(m, "TYPE_CODE"),
				"XYHZ")) {
			m.putAll(timesWorkOrderInfo(MapUtils.getString(m, "ID")));
		}
		return m;

	}

	/**
	 * 通过订单号获取订单信息
	 * 
	 * @param id
	 * @return
	 */
	public Map<String, Object> getOrderInfoByOrderNo(String orderNo) {

		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT ID,ORDER_NO,ORDER_DATE,TYPE_ID TYPE_CODE,TYPE_NAME,TITLE ");
		sbf.append(" ,STATUS,STATUS_NAME,REMARK,date_format(CTIME,'%Y-%m-%d %T') CTIME,date_format(UTIME,'%Y-%m-%d %T') UTIME ,PRICE ");
		sbf.append(" ,CONTACTOR_PERSON,CONTACTOR_TEL CONTACTOR_PHONE,CHARGE_ID ");
		sbf.append(" FROM APP_ORDER_MAIN ");
		sbf.append(" WHERE  ORDER_NO=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { orderNo });
		if (ls.isEmpty()) {
			return new HashMap<String, Object>();
		}
		Map<String, Object> m = ls.get(0);
		return m;

	}

	/**
	 * 预定订单信息
	 * 
	 * @return
	 */
	private Map<String, Object> bookOrderInfo(String id) {
		if (StringUtils.isBlank(id)) {
			return new HashMap<String, Object>();
		}
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT T1.RESERVATION_TIME,T1.STORE_ID,T2.NAME STORE_NAME ");
		sbf.append(" ,T1.RESERVATION_PERSON ");
		sbf.append("  ,T2.ADDRESS STORE_ADDRESS,T2.DESCRIPTION STORE_DESCRIPTION ");
		sbf.append(" FROM APP_ORDER_BOOK T1  ");
		sbf.append(" LEFT JOIN APP_STORE T2 ON T1.STORE_ID=T2.ID ");
		sbf.append(" WHERE T1.ID=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { id });
		if (!ls.isEmpty()) {
			return ls.get(0);
		}
		return new HashMap<String, Object>();
	}

	/**
	 * 外卖订单信息
	 * 
	 * @return
	 */
	private Map<String, Object> takeOutOrderInfo(String id) {
		if (StringUtils.isBlank(id)) {
			return new HashMap<String, Object>();
		}
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT T1.RESERVATION_TIME,T1.STORE_ID ");
		sbf.append(" ,T1.RESERVATION_ADDRESS CONTACTOR_ADDRESS ");
		sbf.append("  ,T1.STORE_ADDRESS ");
		sbf.append(" FROM APP_TAKE_OUT_ORDER T1 ");
		sbf.append(" WHERE T1.ID=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { id });
		if (!ls.isEmpty()) {
			return ls.get(0);
		}
		return new HashMap<String, Object>();
	}

	/**
	 * 挂号订单信息
	 * 
	 * @return
	 */
	private Map<String, Object> registerOrderInfo(String id) {
		if (StringUtils.isBlank(id)) {
			return new HashMap<String, Object>();
		}
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT T1.RESERVATION_TIME,T1.STORE_ID,T2.NAME STORE_NAME ");
		sbf.append("  ,T2.ADDRESS STORE_ADDRESS,T2.DESCRIPTION STORE_DESCRIPTION ");
		sbf.append(" ,T3.NAME  DEPARTMENT_NAME ,T4.NAME PERSON_NAME ");
		sbf.append(" FROM APP_REGISTER_ORDER T1 ");
		sbf.append(" LEFT JOIN APP_STORE T2 ON T1.STORE_ID=T2.ID ");
		sbf.append(" LEFT JOIN APP_STORE_DEPARTMENT T3 ON T1.DEPARTMENT_ID=T3.ID ");
		sbf.append(" LEFT JOIN APP_STORE_DEPARTMENT_PERSON T4 ON T1.PERSON_ID=T4.ID ");
		sbf.append(" WHERE T1.ID=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { id });
		if (!ls.isEmpty()) {
			return ls.get(0);
		}
		return new HashMap<String, Object>();
	}

	/**
	 * 老人专区订单信息
	 * 
	 * @return
	 */
	private Map<String, Object> arrondiOrderInfo(String id) {
		if (StringUtils.isBlank(id)) {
			return new HashMap<String, Object>();
		}
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT T1.ADDRESS CONTACTOR_ADDRESS,T1.AGE,T1.RESERVATION_TIME  ");
		sbf.append(" FROM APP_ARRONDI_ORDER T1 ");
		sbf.append(" WHERE T1.ID=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { id });
		if (!ls.isEmpty()) {
			return ls.get(0);
		}
		return new HashMap<String, Object>();
	}
	
	/**
	 * 接送订单信息
	 * 
	 * @return
	 */
	private Map<String, Object> petProductsOrderInfo(String id) {
		if (StringUtils.isBlank(id)) {
			return new HashMap<String, Object>();
		}
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT T1.ADDRESS CONTACTOR_ADDRESS,T1.SEND_ADDRESS,T1.RESERVATION_TIME ,T1.CAR_TYPE ");
		sbf.append(" ,T2.NAME CAR_NAME ");
		sbf.append(" FROM APP_PET_PRODUCTS_ORDER T1 ");
		sbf.append(" LEFT JOIN APP_CAR_TYPE T2 ON T1.CAR_TYPE=T2.ID ");
		sbf.append(" WHERE T1.ID=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { id });
		if (!ls.isEmpty()) {
			return ls.get(0);
		}
		return new HashMap<String, Object>();
	}
	
	/**
	 * 陪跑订单信息
	 * 
	 * @return
	 */
	private Map<String, Object> withRunOrderInfo(String id) {
		if (StringUtils.isBlank(id)) {
			return new HashMap<String, Object>();
		}
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT T1.RESERVATION_TIME,T1.ADDRESS CONTACTOR_ADDRESS ");
		sbf.append(" FROM APP_WITH_RUN_ORDER T1 ");
		sbf.append(" WHERE T1.ID=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { id });
		if (!ls.isEmpty()) {
			return ls.get(0);
		}
		return new HashMap<String, Object>();
	}

	/**
	 * 
	 * 同城订单
	 * @return
	 */
	private Map<String, Object> sameCityOrderInfo(String id) {
		if (StringUtils.isBlank(id)) {
			return new HashMap<String, Object>();
		}
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT T1.RESERVATION_TIME  ");
		sbf.append(" FROM APP_SAME_CITY_ORDER T1 ");
		sbf.append(" WHERE T1.ID=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { id });
		if (!ls.isEmpty()) {
			return ls.get(0);
		}
		return new HashMap<String, Object>();
	}
	
	/**
	 * 时间工坊订单信息
	 * 
	 * @return
	 */
	private Map<String, Object> timesWorkOrderInfo(String id) {
		if (StringUtils.isBlank(id)) {
			return new HashMap<String, Object>();
		}
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT T1.ID_CARD,T1.RESERVATION_TIME,T1.ADDRESS CONTACTOR_ADDRESS ");
		sbf.append(" FROM APP_TIMES_WORK_ORDER T1 ");
		sbf.append(" WHERE T1.ID=? ");
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(
				sbf.toString(), new Object[] { id });
		if (!ls.isEmpty()) {
			return ls.get(0);
		}
		return new HashMap<String, Object>();
	}

	/**
	 * 更新订单价格和状态
	 */
	public boolean updateOrderStatusPrice(String id, BigDecimal price,
			int status, String statusName) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" UPDATE APP_ORDER_MAIN SET PRICE=? ,STATUS=?, STATUS_NAME=? ,UTIME=? WHERE ID=? ");
		return jdbcTemplate.update(sbf.toString(), new Object[] { price,
				status, statusName,new Date(), id }) > 0;
	}

	/**
	 * 获取订单类型
	 */
	public List<Map<String, Object>> getAllOrderType() {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT ID,CODE,NAME,REMARK FROM APP_ORDER_TYPE WHERE IS_ACTIVE=1 ");
		return jdbcTemplate.queryForList(sbf.toString());
	}

	/**
	 * 回写订单支付charge
	 */
	public void updateOrderCharge(String orderId, String chargeId) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("UPDATE APP_ORDER_MAIN SET CHARGE_ID=? WHERE ID=? ");
		jdbcTemplate.update(sbf.toString(), new Object[] { chargeId, orderId });
	}

	/**
	 * 更新订单状态
	 * 
	 * @param id
	 * @param status
	 * @param statusName
	 * @return
	 */
	public boolean updateStatus(String id, int status, String statusName) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(" UPDATE APP_ORDER_MAIN SET STATUS=? , STATUS_NAME=? ,UTIME=? WHERE ID=? ");
		return jdbcTemplate.update(sbf.toString(), new Object[] { status,
				statusName,new Date(), id }) > 0;
	}

	/**
	 * 插入订单流水记录
	 */
	public boolean insAppPayLog(String id, Charge ch) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("INSERT INTO APP_ORDER_PAY_LOG(ID,PAY_ID,CTIME,CHARGE_INFO,charge_no,charge_id)");
		sbf.append("VALUES(?,?,?,?,?,?)");
		return jdbcTemplate.update(
				sbf.toString(),
				new Object[] { id, ch.getChannel(), new Date(),
						JsonUtil.toJson(ch), ch.getTransactionNo() ,ch.getId()}) > 0;
	}

	/**
	 * 插入支付回调日志
	 */
	public boolean insAppReturnPayLog(Charge ch) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("INSERT INTO APP_RETURN_PAY_LOG(ID,CHARGE_ID,PAY_ID,CTIME,CHARGE_INFO,charge_no,order_no)");
		sbf.append("VALUES(?,?,?,?,?,?,?)");
		return jdbcTemplate.update(
				sbf.toString(),
				new Object[] { UUID.randomUUID().toString(), ch.getId(),
						ch.getChannel(), new Date(), JsonUtil.toJson(ch),
						ch.getTransactionNo(), ch.getOrderNo() }) > 0;
	}
	
	/**
	 * 获取车辆类型
	 */
	public List<Map<String,Object>> getCarType(){
		return jdbcTemplate.queryForList("SELECT ID,NAME FROM APP_CAR_TYPE WHERE IS_ACTIVE=1 ");
	}
}
